/**
 * Created with PhpStorm.
 * User: Administrator
 * Date: 14-1-16
 * Time: 下午4:01
 * Function:
 */

$(".search_form #search_kind a").click(function(){
    $("#act").val("");
    var key=$(this).attr("key");
    $("#"+key).val($(this).attr("kvalue"));
    $(".search_form").submit();
})
$(function(){
    $("#mem_list_tbd").on('dblclick','td', function(){
        if($(this).hasClass('td_dname')) {
            $(this).find('input.dname_input')
                .removeAttr('readonly')
                .addClass('edit_dname')
                .focus();
            return;
        }
        var tr = $(this).parent('tr');
        if(tr.hasClass('hightlight')) {
            tr.removeClass('hightlight');
        } else {
            tr.addClass('hightlight');
        }
    });
    var guid = 0;
    var save = function(){

        var data = {
            action:'DnameUpdate',
            dname:$("#uname").val(),
            com_name:$("#com_name").val(),
            com_type: $("#com_type").find('option:selected').val(),
            dcode: $("#dcode").val(),
            ast : $("#add_saler_type").find('option:selected').val(),
            jiutian_auth : $('input[name="jiutian_auth"]:checked').val(),
            refund_back : $('input[name="refund_back"]:checked').val(),
            group_id : $("#gloup_id").find('option:selected').val(),
            id: guid
        };
//       console.log(data);return;
        if(guid>0 && $("#uname").val().length>0) {
            $.post('/call/jh_mem.php',data, function(res){
                alert(res.msg);
                if(res.status=='ok') {
                    location.reload();
                }
            },'json');
        } else {
            alert("发生错误。企业名称不能为空。")
        }

    };
    $("#mem_list_tbd").on('click','a.edit', function(){
        guid = $(this).attr('data-id');
        var com_types = ['--请选择--','景区','酒店','旅行社','加盟门店','电商','团购网','淘宝/天猫','个人','其他'],
            astList = ['新建','搜索/新建'],
            uname = $("#editInfo_"+guid).find('span.dname').text(),
            com_name = $("#editInfo_c"+guid).find('span.com_name').text(),
            old_value = $("#editInfo_"+guid).find('span.com_type').text(),
            ast = parseInt($("#editInfo_"+guid).attr('data-ast'),10),
            dcode = $("#dcode_"+guid).text(),
            jt_acc = $(this).parent().next().text(),
            acc_right = parseInt($("#editInfo_"+guid).attr('data-jt-acc'),10),
            refund_back_z = parseInt($("#editInfo_"+guid).attr('data-refund_back'),10),
            group_id = parseInt($("#editInfo_"+guid).attr('data-group_id'),10),
            content = '<p>账号名称：<input class="pinput-txt" type="text" id="uname" value="' + $.trim(uname) + '" /></p>',
            options = '',
            options2 = '';
        for(var i=0;i < com_types.length;i++) {
            options += '<option value="'+com_types[i]+'"'+
                (com_types[i]==old_value? 'selected="selected"':'')+'>'+com_types[i]+'</option>';

        }
        for(var j=0;j < astList.length;j++) {
            options2 += '<option value="'+j+'"'+
                (j==ast ? 'selected="selected"':'')+'>'+astList[j]+'</option>';

        }

        content += '<p>企业名称：<input class="com-txt" type="text" id="com_name" value="' + $.trim(com_name) + '"  /></p>';
        content += '<p>企业类型：<select id="com_type">'+options+'</select></p>';
        content += '<p>接口编码：<input class="pinput-txt" type="text" id="dcode" value="'+dcode+'"></p>';
        content += '<p>添加分销：<select id="add_saler_type">'+options2+'</select></p>';
        if(jt_acc=='供应商'){
            content += '<p>绑定闸机权限：<input type="radio"  name="jiutian_auth" value="1" '+(acc_right==1?'checked':'')+'/>允许';
            content += '<input type="radio"  name="jiutian_auth" value="0" '+(acc_right==0?'checked':'')+'/>禁止</p>';

            content += '<p>分销商/散客退款设置：<input type="radio"  name="refund_back" value="1" '+(refund_back_z==1?'checked':'')+'/>支付宝';
            content += '<input type="radio"  name="refund_back" value="0" '+(refund_back_z==0?'checked':'')+'/>资金账户</p>';
        }
        content += '<p>帐号类型：<select name="group_id" id="gloup_id">';
        content += '<option value ="1" '+(group_id==1?'selected="selected"':'')+'">普通帐号</option>';
        content += '<option value ="6" '+(group_id==6?'selected="selected"':'')+'">签约帐号</option>';
        content += '<option value ="2" '+(group_id==2?'selected="selected"':'')+'">测试帐号</option>';
        content += '<option value ="3" '+(group_id==3?'selected="selected"':'')+'">内部点餐帐号</option>';
        content += '<option value ="4" '+(group_id==4?'selected="selected"':'')+'">云顶帐号</option>';
        content += '<option value ="5" '+(group_id==5?'selected="selected"':'')+'">鼓浪屿帐号</option>';
        content += '</select></p>';
        easyDialog.open({
            container : {
                header : '信息修改',
                content : content,
                yesFn : save,
                noFn : true
            }
        });
    });

    //联盟
    $("#mem_list_tbd").on('click','a.alliance', function(){
        guid = $(this).attr('data-id');

        uname = $("#editInfo_"+guid).find('span.dname').text(),
            content = '<form class="alliance">';
        content += '<input type="hidden" name="action" value="transform" />';
        content += '<input type="hidden" name="memberID" value="'+guid+'" />';
        content += '<p>联盟名称：<input class="pinput-txt" type="text" value="' + $.trim(uname) + '"  name="alliName"/></p>',
            // 平台|供应商|盟主|省代|市代|上级邀请人|邀请人
            content += '<p>利润比例：平台<input class="pinput-txt_b" type="text" name="profit_a" value="15">% 供应商<input class="pinput-txt_b" type="text" name="profit_b" value="20">% 联盟<input class="pinput-txt_b" type="text" name="profit_x">% 邀请人<input class="pinput-txt_b" type="text" name="profit_y">% </p>';
        content += '<p>联盟分配：盟主<input class="pinput-txt_b" type="text" name="profit_c" value="25">% 省代理<input class="pinput-txt_b" type="text" name="profit_d" value="15">% 市代理<input class="pinput-txt_b" type="text" name="profit_e" value="10">% </p>';
        content += '<p>邀请人分配：上级邀请人<input class="pinput-txt_b" type="text" name="profit_f" value="10">% 邀请人<input class="pinput-txt_b" type="text" name="profit_g" value="5">%</p>';
        // content += '<p><h4>（盟主：代理商：邀请人）</h4></p>';
        content +='</form>';
        // console.log(content);
        easyDialog.open({
            container : {
                header : '转化联盟',
                content : content,
                yesFn : function(){
                    var data = $(".alliance").serialize();
                    $.getJSON('../call/jh_alliance.php',data,function(json){
                        if(json.code!=100){
                            alert(json.msg);
                            return false;
                        }
                        alert(json.msg);
                        window.location.reload();
                    });
                },
                noFn : true
            }
        });
    });
	// 账号删除
	$("#mem_list_tbd").on("click",".del_account",function(){
		if(confirm("删除账号将清空手机号并把账号删除，无法使用，确定这么做吗?")){
			var params = {action:'clearAccount', mid:$(this).attr("data-mid")};
			$.post("/call/jh_mem.php", params, function(res){
				alert(res.msg);
				window.location.reload();
			},'json');
		}else{
			// alert("取消删除");
		}
	});
	// 重置密码
	$("#mem_list_tbd").on("click",".reset_password",function(){
		if(confirm("是否重置此账号密码为【pft@12301】？")){
			var params = {action:'resetPassword', mid:$(this).attr("data-mid")};
			$.post("/call/jh_mem.php", params, function(res){
				alert(res.msg);
			},'json');
		}
	});
    $("#mem_list_tbd").on('blur','input.dname_input', function(){
        $(this).attr('readonly','readonly').removeClass('edit_dname');
    });
    $("#ExcelBtn").on('click',function(){
        $("#act").val("loadExcel");
        $(".search_form ").submit();
    });
    $("#searchBtn").click(function(){
        $("#act").val("")  ;
        $(".search_form").submit();
    });
    $("#mem_list_tbd").on('click', 'a.verify', function(){
        if(!confirm('您确定要执行此操作？')) return;
        var status = $(this).attr('data-status'),
            parent_obj = $(this).parent('td'),
            mid = parent_obj.siblings('td.mid').attr('data-mid'),
            params = {action:'SetMemStatus', status:status, mid:mid};
        $.post("/call/jh_mem.php", params, function(res){
            alert(res.msg);
            if(res.status=='ok') {
                parent_obj.siblings('td.status').html(res.body);
            }
        },'json');
    });
    $('#mem_list_tbd').on('click','a.saleID',function(e){
        var d_this = $(e.currentTarget);
        var union_id   = d_this.parent().siblings('.mid').attr('data-mid');
        $("input[name='z_salesID']")[0].value=union_id;
        $(".black").css({
            "width":$(window).width()+"px",
            "height":$(window).height()+"px",
            "display":"block"
        });
        PFT_GLOBAL.G.Ajax({
            url : "../module/zax/admin_report/call.php",
            data : {action:'get_salesID',fid:union_id},
            type :'POST',
            timeout : function(){console && console.log("获取数据超时")},
            serverError : function(){console &&
            console.log("接口出错:module/zax/admin_report/call.php?get_saleID")}
        },function(res){
            $("#salesID>option" ).each(function() {
                if($(this)[0].value==res.salesID){
                    $(this).attr('selected','selected');
                    return false;
                }
            })
            $("#kefuID>option" ).each(function() {
                if($(this)[0].value==res.kefuID){
                    $(this).attr('selected','selected');
                    return false;
                }
            })
//            res.protocal_start = '2015-12-20';
//            res.protocal_end   = '2015-12-31';
//            res.protocol_main  = '年费9800,协议截止日期为2016年12月31日';
            $("#protocol_start").val(res.protocol_start);
            $("#protocol_end").val(res.protocol_end);
            $("#protocol_main").val(res.protocol_main);
            $(".alert_box").css("display","block");
        })
    })

    $(".click_close,.cancel").click(function(){
        $(".alert_box").css("display","none");
        $(".black").css("display","none");
    })
    $(".save").click(function(){
        var union_id = $("input[name='z_salesID']")[0].value,
         salesID = $("#salesID>option:selected")[0].value,
         kefuID = $("#kefuID>option:selected")[0].value,
         protocal_start = $("#protocol_start").val(),
         protocal_end   = $("#protocol_end").val(),
         protocol_main  = $("#protocol_main").val();
        PFT_GLOBAL.G.Ajax({
            url : "../module/zax/admin_report/call.php",
            data : {
                action:'single_salesID',
                fid:union_id,
                salesID:salesID,
                kefuID:kefuID,
                protocal_start:protocal_start,
                protocal_end:protocal_end,
                protocol_main:protocol_main
            },
            type :'POST',
            timeout : function(){console && console.log("获取数据超时")},
            serverError : function(){console &&
            console.log("接口出错:module/zax/admin_report/call.php?single_salesID")}
        },function(res){
            if(res.sta=='suc')
                PFT_GLOBAL.U.Alert("success",'<p style="width:120px;">设置成功</p>');
            else
                PFT_GLOBAL.U.Alert("fail",'<p style="width:120px;">设置失败</p>');
            $(".alert_box").css("display","none");
            $(".black").css("display","none");
        })

       })
});